//
//  GNRPickCard.m
//  FBSnapshotTestCase
//
//  Created by LvYuan on 2017/10/31.
//

#import "GNRPickCard.h"

@interface GNRPickCard ()

@end

@implementation GNRPickCard
@synthesize maskView,superView,navigationSuperView,contentSuperView,actionSuperView,bottomExtendView;

//MARK: - Private
+ (UIView *)productView{
    UIView *view = UIView.new;
    return view;
}

- (void)dealloc{
    GNRAlertLog(@"");
}

- (instancetype)init{
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return self;
}

- (void)tapPressed:(id)sender{
    if (self.disableDismissTapBGView) {//禁用点击背景消失
        return;
    }
    if ([self.delegate respondsToSelector:@selector(closePressedPickCard:)]) {
        [self.delegate closePressedPickCard:self];
    }
    //block
    if (_closePressBlock) {
        _closePressBlock(self);
    }
}

- (UIButton *)maskView{
    if (!maskView) {
        maskView = [UIButton buttonWithType:UIButtonTypeSystem];
        [maskView addTarget:self action:@selector(tapPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:maskView];
    }
    return maskView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
    [self initData];
    [self installMainUI];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self showAnimation:YES];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self dismissAnimation:YES];
}

- (void)showAnimation:(BOOL)animation{
    GNRWEAK_SELF;
    [UIView animateWithDuration:0.3 animations:^{
        [wself.superView mas_updateConstraints:^(MASConstraintMaker *make) {
            if (@available(iOS 11.0, *)) {
                make.bottom.equalTo(wself.view.mas_safeAreaLayoutGuideBottom);
            } else {
                make.bottom.equalTo(wself.view.mas_bottom).offset(0);
            }
        }];
        [wself.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)dismissAnimation:(BOOL)animation{
    GNRWEAK_SELF;
    [UIView animateWithDuration:0.2 animations:^{
        [wself.superView mas_updateConstraints:^(MASConstraintMaker *make) {
            if (@available(iOS 11.0, *)) {
                make.bottom.equalTo(wself.view.mas_safeAreaLayoutGuideBottom).offset(superViewTotalHeight);;
            } else {
                make.bottom.equalTo(wself.view.mas_bottom).offset(superViewTotalHeight);
            }
        }];
        [wself.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

//MARK: - Getter
- (UIView *)superView{
    if (!superView) {
        superView = [GNRPickCard productView];
        superView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:superView];
    }
    return superView;
}

- (UIView *)navigationSuperView{
    if (!navigationSuperView) {
        navigationSuperView = [GNRPickCard productView];
        [self.superView addSubview:navigationSuperView];
    }
    return navigationSuperView;
}

- (UIView *)contentSuperView{
    if (!contentSuperView) {
        contentSuperView = [GNRPickCard productView];
        [self.superView addSubview:contentSuperView];
    }
    return contentSuperView;
}

- (UIView *)actionSuperView{
    if (!actionSuperView) {
        actionSuperView = [GNRPickCard productView];
        [self.superView addSubview:actionSuperView];
    }
    return actionSuperView;
}

- (UIView *)bottomExtendView{
    if (!bottomExtendView) {
        bottomExtendView = [GNRPickCard productView];
        bottomExtendView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:bottomExtendView];
    }
    return bottomExtendView;
}

- (void)initData{
    
    if (self.navBarConfiger) {
        navigationBarHeight = [self.navBarConfiger respondsToSelector:@selector(contentHeight)]?self.navBarConfiger.contentHeight:0;
    }
    
    if (self.contentConfiger) {
        contentViewHeight = [self.contentConfiger respondsToSelector:@selector(contentHeight)]?self.contentConfiger.contentHeight:0;
    }
    
    if (self.actionConfiger) {
        actionViewHeight = [self.actionConfiger respondsToSelector:@selector(contentHeight)]?self.actionConfiger.contentHeight:0;
    }
    superViewTotalHeight = navigationBarHeight + contentViewHeight + actionViewHeight;
}

- (void)installMainUI{
    
    GNRWEAK_SELF;
    //maskView
    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    //superView
    [self.superView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wself.view.mas_left).offset(0);
        make.right.equalTo(wself.view.mas_right).offset(0);
        make.bottom.equalTo(wself.view.mas_bottom).offset(superViewTotalHeight);
        make.height.mas_equalTo(superViewTotalHeight);
    }];
    
    //bottomExtendView
    [self.bottomExtendView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wself.view.mas_left).offset(0);
        make.right.equalTo(wself.view.mas_right).offset(0);
        make.top.equalTo(wself.superView.mas_bottom).offset(0);
        make.bottom.equalTo(wself.view.mas_bottom).offset(0);
    }];
    
    //navigation
    [self.navigationSuperView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wself.superView.mas_left).offset(0);
        make.right.equalTo(wself.superView.mas_right).offset(0);
        make.top.equalTo(wself.superView.mas_top).offset(0);
        make.height.mas_equalTo(navigationBarHeight);
    }];
    
    UIView *navigationBar = self.navBarConfiger.contentView;
    [self.navigationSuperView addSubview:navigationBar];
    [navigationBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    //content
    [self.contentSuperView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wself.superView.mas_left).offset(0);
        make.right.equalTo(wself.superView.mas_right).offset(0);
        make.top.equalTo(wself.navigationSuperView.mas_bottom).offset(0);
        make.height.mas_equalTo(contentViewHeight);
    }];
    
    UIView *contentView = self.contentConfiger.contentView;
    [self.contentSuperView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    //contentView shown
    if (contentView) {
        //delegate
        if ([self.delegate respondsToSelector:@selector(contentViewShown:pickCard:)]) {
            [self.delegate contentViewShown:contentView pickCard:self];
        }
        if (self.contentViewShownCompletion) {
            self.contentViewShownCompletion(self, contentView);
        }
    }

    //action
    [self.actionSuperView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wself.superView.mas_left).offset(0);
        make.right.equalTo(wself.superView.mas_right).offset(0);
        make.top.equalTo(wself.contentSuperView.mas_bottom).offset(0);
        make.height.mas_equalTo(actionViewHeight);
    }];
    
    UIView *actionView = self.actionConfiger.contentView;
    [self.actionSuperView addSubview:actionView];
    [actionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];

    [self.view layoutIfNeeded];
}

//MARK: - Protocol
- (id<GNRPickCardBaseViewProtocol>)navBarConfiger{return nil;}
- (id<GNRPickCardBaseViewProtocol>)contentConfiger{return nil;}
- (id<GNRPickCardBaseViewProtocol>)actionConfiger{return nil;}

- (BOOL)disableDismissTapBGView{
    return _disableDismissTapBGView;
}

- (void)setDisableDismissTapBGView:(BOOL)disableDismissTapBGView{
    _disableDismissTapBGView = disableDismissTapBGView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
